// Copyright 2020 Poofee (https://github.com/Poofee)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
/*****************************************************************************
 *                                                                           *
 *  File:    to5relay.cpp                                                    *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *  Authors: Poofee                                                          *
 *  Email:   poofee@qq.com                                                   *
 *  Address: https://github.com/Poofee                                       *
 *  Date:    2020年07月15日                                                   *
 *                                                                           *
 *****************************************************************************/
#include "to5relay.h"


/**
 * @brief
 *
 */
to5Relay::to5Relay() {

}


/**
 * @brief
 *
 */
to5Relay::~to5Relay() {

}


/**
 * @brief
 *
 */
void to5Relay::init() {
    /** 文件名，需不带扩展名 **/
    setFileName((char*)"../geo/to5/to5newair");
    MAX_NONLINEARSTEPS = 100;
    /** 设置材料参数 **/
    CMaterial* air_material = new CMaterial;
    materialList.emplace_back(air_material);

    CMaterial* pm_material = new CMaterial;
    materialList.emplace_back(pm_material);
    //pm_material->H_c = 1e6;
    pm_material->miu *= 1.32;
    PermanentMagnet* pm = new PermanentMagnet;
    pm->type = 0;
    pm->mur = 1.32;
    pm->Br = 1.05;
    pm->Hc = pm->Br / pm->mur / miu0;
    pm->v[0] = 0;
    pm->v[1] = -1;
    pm->v[2] = 0;
    pm_material->pm = pm;

    CMaterial* dt4e_material = new CMaterial;
    materialList.emplace_back(dt4e_material);
	//dt4e_material->miu *= 4000;
    dt4e_material->sigma = 0;//1 / (2e-7);
    int num_bh = 46;
    dt4e_material->BHpoints = num_bh;
	dt4e_material->Hdata = (double*)malloc(num_bh * sizeof(double));
	dt4e_material->Bdata = (double*)malloc(num_bh * sizeof(double));
    /** BH曲线 **/
    double H[] = { 0,428.571428571429,857.142857142857,1285.71428571429,1714.28571428571,2142.85714285714,2571.42857142857,3000,3500,4413.79310344828,5327.58620689655,6241.37931034483,7155.17241379310,8068.96551724138,8982.75862068966,9896.55172413793,10810.3448275862,11724.1379310345,12637.9310344828,13551.7241379310,14465.5172413793,15379.3103448276,16293.1034482759,17206.8965517241,18120.6896551724,19034.4827586207,19948.2758620690,20862.0689655172,21775.8620689655,22689.6551724138,23603.4482758621,24517.2413793103,25431.0344827586,26344.8275862069,27258.6206896552,28172.4137931035,29086.2068965517,30000,35000,72857.1428571429,110714.285714286,148571.428571429,186428.571428571,224285.714285714,262142.857142857,300000 };
    double B[] = { 0,0.372226347720340,0.718255801655239,1.02151937975884,1.27727668162527,1.48879436525801,1.66269042654969,1.80600236239019,1.94275062464612,2.13137075612274,2.26665591661634,2.36762589943982,2.44556850663998,2.50744623596205,2.55773052112890,2.59940194512075,2.63451400059535,2.66452293044522,2.69048782802663,2.71319615611398,2.73324490015708,2.75109448216963,2.76710546161188,2.78156407144735,2.79470033927051,2.80670117739633,2.81771999239898,2.82788384377183,2.83729884866387,2.84605431274931,2.85422592323703,2.86187824270231,2.86906667561760,2.87583903292624,2.88223678714567,2.88829608699090,2.89404858350831,2.89952210727271,2.92549005076425,3.03457600188034,3.10162551915606,3.15875244833436,3.21199980409379,3.26333173070399,3.31357797189484,3.36314950051050 };
	for (int ih = 0; ih < num_bh; ++ih) {
		dt4e_material->Hdata[ih] = H[ih];
		dt4e_material->Bdata[ih] = B[ih];
	}

    CMaterial* coil_material = new CMaterial;
    materialList.emplace_back(coil_material);

    Coil* coil = new Coil;
    coil->xyz       = 1;
    coil->direction = -1;
    coil->Nc        = 3000;
    coil->Rc        = 2200;
    coil->Vc        = 27;
    coil->type      = CICULARCOIL;
    coil->height    = 2.5e-3;
    coil->center[0] = 0;
    coil->center[1] = -1.8e-3;
    coil->center[2] = -2.1e-3;
    coil->r0        = 0.85e-3;
    coil->width     = 0.4e-3;
    /** 线圈磁场应向下-27 **/
	coil->Jor = coil->Vc / coil->Rc * coil->Nc / coil->width / coil->height;
    coil_material->coil = coil;
    coil_material->tau = coil->Nc / coil->width / coil->height;

    CMaterial* coil_material1 = new CMaterial;
    materialList.emplace_back(coil_material1);

    Coil* coil1 = new Coil;
    coil1->xyz       = 1;
    coil1->direction = -1;
    coil1->Nc        = 3000;
    coil1->Rc        = 2220;
    coil1->Vc        = 27;
    coil1->type      = CICULARCOIL;
    coil1->height    = 2.5e-3;
    coil1->center[0] = 0;
    coil1->center[1] = -1.8e-3;
    coil1->center[2] = 2.1e-3;
    coil1->r0        = 0.85e-3;
    coil1->width     = 0.4e-3;
	/** 线圈磁场应向下 **/
    coil1->Jor = coil1->Vc / coil1->Rc * coil1->Nc / coil1->width / coil1->height;
    coil_material1->coil = coil1;
    coil_material1->tau = coil1->Nc / coil1->width / coil1->height;


    /** 设置映射关系 **/
	setMaterial(1, dt4e_material);/** 衔铁 **/
    setMaterial(2, pm_material);/** 永磁 **/
	setMaterial(3, dt4e_material);/** 铁芯 **/
    setMaterial(4, coil_material1);/** 线圈coil_material1 **/
	setMaterial(5, air_material);/** 线圈coil_material **/
	setMaterial(6, air_material);/** 空气 **/
    /** 设置形变区域 **/
	setXiantieTag(1);
    setAirTag(5);/** 建议将空气区域或者不想导出的区域一起放在后面 **/
	setRemeshTag(6);

    motion.type = ROTATEMOTION;
    motion.p[0] = 1.74579962392938;/** 单位mm，与geo文件保持一致 **/
    motion.p[1] = -4.10996380979215;
    motion.p[2] = 0.00232963976730521;
    motion.v[0] = 1;
    motion.v[1] = 0;
    motion.v[2] = 0;

    Precision = 1e-5;

    ParallelThreads = 4;
}


/*!
 * @brief      TO-5静态初始化
 * @author     Poofee
 * @date       2020年10月24日
 * @param[out] 
 * @return     void  
**/
void to5Relay::StaticInitial() {
	printf("TO-5 StaticInitial\n");
	init();
    setPrefixName((char*)"static");

	/** 初始化时间步长 **/
	current_step = 0;
	min_time = 0;
	max_time = 0;
	timesteps.resize(8);

	/** 初始化变量 **/
	current_step = 0;
	initPhysicalVariables(timesteps.size());

	/** 设置位移增量列表 **/
	for (int i = 1; i < 8; ++i) {
		timesteps[i] = i;
		Ddisplacements[i] = 0.5;
	}
}

/*!
 * @brief      TO-5瞬态初始化
 * @author     Poofee
 * @date       2020年10月24日
 * @param[out] 
 * @return     void  
**/
void to5Relay::TransientInitial() {
	printf("TO-5 TransientInitial\n");
	init();
    setPrefixName((char*)"eddy");
	/** 初始化时间步长 **/
	current_step = 0;
	min_time = 0;
	max_time = 9e-4;
	timesteps.emplace_back(0);
	for (double t = 0; t <= 9e-4; t += 3.75e-5) {
		timesteps.emplace_back(3.75e-5);
	}

	/** 初始化变量 **/
	current_step = 0;
	initPhysicalVariables(timesteps.size());
	/** 初始化位移，用来测试和验证 **/
    double dis[] = {0,         0, 0, 0, 0.02692, 0.105850, 0.252040, 0.476120, 0.779250, 1.15783, 1.60125, 2.10393, 2.66475, 3.28695, 3.580, 3.580, 3.580, 3.580, 3.580, 3.580, 3.580, 3.58, 3.58, 3.58, 3.58, 3.58};
	for (int i = 1; i < timesteps.size(); ++i) {
        displacements[i] = dis[i];
        Ddisplacements[i] = displacements[i] - displacements[i - 1];
    }
}




